package com.linewow.xhyy.nihaonew.binder;
import android.databinding.BindingAdapter;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;

import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.entity.NewTopicImageInfo;
import com.linewow.xhyy.nihaonew.model.HttpModel;
import com.squareup.picasso.Picasso;

import java.io.File;
/**
 * Created by LXR on 2016/8/30.
 */
public class CustomBind {
    @BindingAdapter({"bind:imagePath"})
    public static void loadImgByPath(ImageView view, String imagePath){
        Picasso.with(view.getContext()).load(imagePath).error(R.mipmap.ic_launcher)
                .into(view);
    }
    @BindingAdapter({"bind:imagePathPlus"})
    public static void loadImgByPathPlus(ImageView view, String imagePath){
        Picasso.with(view.getContext()).load(HttpModel.IMGPRE+imagePath)
                .into(view);
    }

    @BindingAdapter({"bind:imagePathTag"})
    public static void loadImgByPathTag(ImageView view, String imagePath){
        Picasso.with(view.getContext()).load(HttpModel.HOMETAGIMG+imagePath).fit().placeholder(R.mipmap.bg_tag_list).error(R.mipmap.bg_tag_list)
                .into(view);
    }

    @BindingAdapter({"bind:imagePathFile"})
    public static void loadImgByPathFile(ImageView view, String imagePath){
        Picasso.with(view.getContext()).load(new File(imagePath))
                .into(view);
    }

    @BindingAdapter("bind:afterTextChanged")
    public static void setNewTopicImageInfo(EditText editText, final NewTopicImageInfo info){

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                info.setDescription(editable.toString());
            }
        });
    }
}
